package com.beici.waterghost.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beici.waterghost.mapper.fansMapper;
import com.beici.waterghost.model.fans;
import com.beici.waterghost.model.node;
import com.beici.waterghost.model.resultes;
import com.beici.waterghost.utils.takeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.List;

@Service
public class fansServiceImpl {
    @Autowired
    private fansMapper fansServer;

    @Autowired
    private nodeServiceImpl nodeService;

    public resultes addFans(Integer userId,Integer fansId){
        try {
            int insert = fansServer.insertFans(userId,fansId);
            return insert>0? resultes.success("关注成功",true):resultes.error("博主不存在");
        }catch (Exception e){
            e.printStackTrace();
            return resultes.error("您已关注过该博主");
        }
    }

    // 查看当前对象是否为自己关注的对象
    public boolean isFans(Integer userId,Integer fansId){
        Integer integer = fansServer.selectCount(new QueryWrapper<fans>().eq("fans_id", fansId).eq("user_id", userId));
        return integer>0?true:false;
    }

    // 获取关注博客的最新帖子
    public resultes getBZNodes(Integer fansId){
        List<fans> fans = fansServer.selectList(new QueryWrapper<fans>().eq("fans_id", fansId));
        List<node> list = new ArrayList<>();
        for (com.beici.waterghost.model.fans fan : fans) {
            resultes resultes = nodeService.todayFriendsNode(fan.getUserId());
            List<node> data = (List<node>) resultes.getData();
            for (node datum : data) {
                list.add(datum);
            }
        }
        return resultes.success("获取成功",list);
    }
}
